#include "data_deal.h"
#include "logic.h"
#include "stdlib.h"
#include <string>
using namespace std;
bool dealFlag = false;
extern "C" void Uart1_Data_Deal()
{
    u1Buff[u1Len] = '\0';
    string ss = (char*)u1Buff;
    u1Len = 0;
    int firstPosition = ss.find("GET:");
    int secondPosition = ss.find("##");
    if(firstPosition != -1 && secondPosition != -1)
    {
        //get id.
        int tmpFirst = ss.find("[");
        int tmpSecond = ss.find("]");
        if(tmpFirst != -1 && tmpSecond != -1)
        {
            uint8_t tmpAddr = atoi(ss.substr(tmpFirst+1,tmpSecond-tmpFirst-1).c_str());
            if(tmpAddr != gSrcAddr && tmpAddr != 0)
            {
                return;
            }
        }
        dealFlag = true;
        ss = ss.substr(firstPosition+4,tmpFirst-firstPosition-4);
        if(ss == "RS485")    gState = INTERRUPT_FROM_485;
        else if(ss == "LORA")gState = INTERRUPT_FROM_LORA;
        else if(ss == "TIME")
        {
            Get_Rtc_Time();
            printf("TIME:(%04d-%02d-%02d %02d:%02d:%02d)[%d]\r\n",gData.Year+2000,\
                gData.Month,gData.Date,gTime.Hours,gTime.Minutes,gTime.Seconds,gSrcAddr);
        }
        else if(ss == "DSTADDR")
        {
            printf("DST:(%d)[%d]\r\n",(uint8_t)Device_FLASH_Read(M_FLASH_ADDR),gSrcAddr);
        }
        else if(ss == "SRCADDR")
        {
                printf("SRC:(%d)[%d]\r\n",gSrcAddr,gSrcAddr);
        }
        else if(ss == "BATTERY")
        {
                printf("bt:(%d%%)[%d]\r\n",100,gSrcAddr);
        }
        else 
        {
            printf("wrong cmd[%d]\r\n",gSrcAddr);
        }
    }
    firstPosition = ss.find("SET:");
    if(firstPosition != -1 && secondPosition != -1)
    {        
        //get id.
        int tmpFirst = ss.find("[");
        int tmpSecond = ss.find("]");
        if(tmpFirst != -1 && tmpSecond != -1)
        {
            uint8_t tmpAddr = atoi(ss.substr(tmpFirst+1,tmpSecond-tmpFirst-1).c_str());
            if(tmpAddr != gSrcAddr && tmpAddr != 0)
            {
                return;
            }
        }
        dealFlag = true;
        ss = ss.substr(firstPosition+4,tmpFirst-firstPosition-4);
        if(ss.find("TIME") != -1)
        {
            firstPosition = ss.find("(");
            secondPosition = ss.find(")");
            if(firstPosition != -1 && secondPosition != -1)
            {
                ss = ss.substr(firstPosition+1,secondPosition-firstPosition-1);
                secondPosition = ss.find(",");
                string tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gData.Year = atoi(tmp.c_str())-2000;
                secondPosition = ss.find(",");
                tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gData.Month = atoi(tmp.c_str());
                //date
                secondPosition = ss.find(",");
                tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gData.Date = atoi(tmp.c_str());
                //hour
                secondPosition = ss.find(",");
                tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gTime.Hours = atoi(tmp.c_str());
                //min
                secondPosition = ss.find(",");
                tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gTime.Minutes = atoi(tmp.c_str());
                //sec
                secondPosition = ss.find(",");
                tmp = ss.substr(0,secondPosition);
                ss = ss.substr(secondPosition+1);
                gTime.Seconds = atoi(tmp.c_str());
                Set_Rtc_Time();
                printf("time set success\r\n");            
                printf("TIME:(%04d-%02d-%02d %02d:%02d:%02d)[%d]\r\n",gData.Year+2000,\
                gData.Month,gData.Date,gTime.Hours,gTime.Minutes,gTime.Seconds,gSrcAddr);

            }
        }
        else if(ss.find("DSTADDR") != -1)
        {
            firstPosition = ss.find("(");
            secondPosition = ss.find(")");
            if(firstPosition != -1 && secondPosition != -1)
            {
                ss = ss.substr(firstPosition+1,secondPosition-firstPosition-1);
            }
            int tmpAddr = atoi(ss.c_str());
            if(tmpAddr >= 256 ) 
            {
                tmpAddr = 0;
                printf("wrong addr[%d]\r\n",gSrcAddr);
            }
            else
            {
                Device_FLASH_Program(FLASH_TYPEPROGRAMDATA_BYTE,M_FLASH_ADDR,(uint64_t)tmpAddr);
                printf("addr set success:(%d)[%d]\r\n",tmpAddr,gSrcAddr);
            }
        }
        else 
        {
            printf("wrong cmd[%d]\r\n",gSrcAddr);
        }
    }
    if(!dealFlag) printf("wrong cmd[%d]\r\n",gSrcAddr);
    dealFlag = false;
}
